package com.chengliang.bbs.controller;

import com.chengliang.bbs.entry.Collection;
import com.chengliang.bbs.entry.JSONResult;
import com.chengliang.bbs.entry.User;
import com.chengliang.bbs.entry.UserAuths;
import com.chengliang.bbs.service.CollectionService;
import com.chengliang.bbs.service.JieServsice;
import com.chengliang.bbs.util.UserUtils;
import com.github.pagehelper.Page;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

/**
 * 收藏
 * @author chengliang
 */
@Slf4j
@Controller
@RequestMapping("/collection")
public class CollectionController {

    private final CollectionService collectionService;

    private final JieServsice jieServsice;

    @Autowired
    public CollectionController(CollectionService collectionService, JieServsice jieServsice) {
        this.collectionService = collectionService;
        this.jieServsice = jieServsice;
    }

    /**
     * 查找帖子是否被收藏
     * @param jieId
     * @return
     */
    @PostMapping("/find")
    @ResponseBody
    public JSONResult find(Integer jieId){
        UserAuths userAuths = UserUtils.currentUser();
        if(userAuths == null){
            return JSONResult.success("请进行登录").append("isShow",false);
        }
        //当前用户ID
        Integer userId = userAuths.getUser().getUserId();
        //是否是自己的帖子
        User user = jieServsice.getJieUserByJieId(jieId);
        if(user == null || userId.equals(user.getUserId())){
            return JSONResult.success("不能收藏自己的帖子").append("isShow",false);
        }
        //已收藏该贴
        if(collectionService.isCollection(userId,jieId)){
            return JSONResult.success().append("collection",true);
        }
        return JSONResult.success().append("collection",false);
    }

    /**
     * 收藏帖子
     * @param jieId
     * @return
     */
    @PostMapping("/add")
    @ResponseBody
    public JSONResult add(Integer jieId){
        //当前用户ID
        Integer userId = UserUtils.currentUser().getUser().getUserId();
        if(userId.equals(jieServsice.getJieUserByJieId(jieId).getUserId())){
            return JSONResult.error("不能收藏自己的帖子哦");
        }
        return JSONResult.is(collectionService.addCollection(jieId,userId),"收藏");
    }

    /**
     * 取消收藏
     * @param jieId
     * @return
     */
    @PostMapping("/remove")
    @ResponseBody
    public JSONResult remove(Integer jieId){
        //当前用户ID
        Integer userId = UserUtils.currentUser().getUser().getUserId();
        return JSONResult.is(collectionService.removeCollection(jieId,userId),"取消收藏");
    }

    /**
     * 我的收藏json数据
     * @param page
     * @param limit
     * @return
     */
    @GetMapping("/list")
    @ResponseBody
    public JSONResult jsonList(@RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "10") Integer limit){
        Page<Collection> collection = collectionService.getMyCollection(UserUtils.currentUser().getUser().getUserId(), page, limit);
        return JSONResult.success(collection).append("count",collection.getTotal());
    }

}
